<template>
  <div class="search-form">
    <el-form ref="formRef" inline :model="form">
      <el-form-item>
        <el-input v-model="form.keyword" clearable placeholder="请输入姓名/手机号" :prefix-icon="Search" @keyup.enter="handleSearch" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { Search } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus'
import { ref } from 'vue'
import type { SearchForm } from '../types'

const emit = defineEmits<{
  (e: 'search', form: SearchForm): void
  (e: 'reset'): void
}>()

// 表单引用
const formRef = ref<FormInstance>()

// 表单数据
const form = ref<SearchForm>({
  keyword: '',
})

// 搜索
const handleSearch = () => {
  emit('search', form.value)
}

// 重置基础搜索
const handleReset = () => {
  formRef.value?.resetFields()
  emit('reset')
}
</script>

<style scoped>
.search-form {
  padding: 16px;
  background-color: #fff;
  border-bottom: 1px solid #ebeef5;
}

:deep(.el-form--inline .el-form-item) {
  margin-right: 16px;
  margin-bottom: 0;
}
</style>
